# Maintainer: Ilya Rakhlin <i.rakhlin@gmail.com>

_realname=pkgconf
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
epoch=1
pkgver=1.8.0
pkgrel=2
pkgdesc='pkg-config compatible utility which does not depend on glib'
url='https://github.com/pkgconf/pkgconf'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
license=('ISC')
options=('!debug' 'strip')
provides=("${MINGW_PACKAGE_PREFIX}-pkg-config")
conflicts=("${MINGW_PACKAGE_PREFIX}-pkg-config")
replaces=("${MINGW_PACKAGE_PREFIX}-pkg-config")
makedepends=("${MINGW_PACKAGE_PREFIX}-meson" "${MINGW_PACKAGE_PREFIX}-cc")
groups=("${MINGW_PACKAGE_PREFIX}-toolchain")
source=("https://github.com/pkgconf/pkgconf/archive/refs/tags/$_realname-$pkgver.tar.gz"
        0002-size-t-format.patch
        0003-printf-format.patch
        0004-default-pure-static.patch
        0001-pkgconf_pkg_parser_value_set-fix-code-path-ordering-.patch)
sha256sums=('d84a2a338a17e0f68e6a8d6c9caf40d6a2c9580c4ae1d475b338b8d956e483aa'
            '1db5975d6b1db946ba1d71cc3cf390dec567899a89d7ef53f56f24f0058cdbe3'
            'a7efeef61b4af1c0813d9d8a211cde669a3e3fa2c97ed21e55487667769c567a'
            '93a4aa1475122d16b6ac8a84f771e004e1a53e1df982d3dd069b262c9815ebb4'
            '5e081aabaaf7a2f9c2f7b9bc887fe8600959c7de540cfcf12cf58c583c320662')

prepare() {
  mv "${_realname}-${_realname}-${pkgver}" "${_realname}-${pkgver}"
  cd ${srcdir}/$_realname-$pkgver

  # https://github.com/msys2/MINGW-packages/issues/8473
  patch -R -p1 -i ${srcdir}/0004-default-pure-static.patch

  # https://github.com/msys2/MINGW-packages/issues/9602
  patch -R -p1 -i ${srcdir}/0001-pkgconf_pkg_parser_value_set-fix-code-path-ordering-.patch

  patch -p1 -i ${srcdir}/0002-size-t-format.patch
  patch -p1 -i ${srcdir}/0003-printf-format.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="--prefix=" \
  ${MINGW_PREFIX}/bin/meson \
    --prefix="${MINGW_PREFIX}" \
    --buildtype=plain \
    -Dtests=false \
    ../${_realname}-${pkgver}

  meson compile
}

package() {
  cd ${srcdir}/build-${MINGW_CHOST}

  DESTDIR="${pkgdir}" meson install

  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkgconf "${pkgdir}${MINGW_PREFIX}"/bin/pkg-config
  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkgconf "${pkgdir}${MINGW_PREFIX}"/bin/${MINGW_CHOST}-pkgconf
  cp "${pkgdir}${MINGW_PREFIX}"/bin/pkg-config "${pkgdir}${MINGW_PREFIX}"/bin/${MINGW_CHOST}-pkg-config
}
